package Servlet.List;

import Server.List.novelShowServer;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "novelShowServlet", value = "/novelShowServlet")
public class novelShowServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        // 通过ServletContext对象获取文件的真实路径
        String realPath = context.getRealPath("");
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String url="reader.jsp";
        HttpSession session = request.getSession();
        String opt = request.getParameter("opt");
        String searchChapter = request.getParameter("searchChapter");
        try {
            novelShowServer server= new novelShowServer(request,response,realPath);

            if(searchChapter!=null && !searchChapter.equals("")){
                server.assignedPage();
            } else if(opt.equals("next")){
//                下一页
                server.nextPage();
            }else if(opt.equals("prev")){
//                上一页
                server.previousPage();
            }
            //冷处理
            session.setAttribute("page" , server.getReaderPilot());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {


            request.getRequestDispatcher(url).forward(request, response);		//转发
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
